(function () {

    //地理信息
    var location = document.getElementById('location');

    //美食信息
    var foodImg = document.getElementsByClassName('food-img');
    var foodName = document.getElementsByClassName('food-name');
    var foodContent = document.getElementsByClassName('food-content');
    var foodCheck = document.getElementsByClassName('food-check');

    //景点信息
    var viewImg = document.getElementsByClassName('view-img');
    var viewName = document.getElementsByClassName('view-name');
    var viewCheck = document.getElementsByClassName('view-check');

    //帖子信息
    var cardImg = document.getElementsByClassName('card-img');
    var cardContent = document.getElementsByClassName('card-content');
    var cardCheck = document.getElementsByClassName('card-check');

    //当前城市
    var currentCity = '';

    //数据列表
    var foodList;
    var viewList;
    var cardList;

    //登陆成功显示头像- TODO为每个页面都有的功能，加完首页的记得给别的页面也添上修改导航右部的功能
    var user_Inf = localStorage.getItem("userInfo");
    var div_before = document.getElementById('login-register_before');
    var div_after = document.getElementById('login-register_after');
    var userImg = document.getElementById('user-Img');

    var page = 0;

    function checkLocalStorage() {
        if (user_Inf == null) {
            div_before.style.setProperty('display','block');
            div_after.style.setProperty('display','none');
        }
        else{
            console.log(div_before.style.display);
            div_before.style.setProperty('display','none');
            div_after.style.setProperty('display','block');
            if(user_Inf.headImg != "")
                userImg.src = user_Inf.headImg;
        }
    }

    function initMap() {
        var map = new AMap.Map('amap_container', {
            zoom: 10,
            mapStyle: 'amap://styles/normal'
        });

        AMap.plugin(['AMap.ToolBar', 'AMap.Scale', 'AMap.Geolocation', 'AMap.OverView'], function () {
            map.addControl(new AMap.ToolBar());
            map.addControl(new AMap.Scale());
            map.addControl(new AMap.Geolocation());
            map.addControl(new AMap.OverView({isOpen: true}));
        });
    }

    function initListener() {
        for (var i = 0; i < 2; i++){
            (function (i) {
                foodCheck.item(i).addEventListener('click', function () {
                    CommonUtil.setCookie('checkFood', foodList[i]);
                    //TODO 跳转到美食列表
                    // window.location.href = '';
                });
                viewCheck.item(i).addEventListener('click', function () {
                    CommonUtil.setCookie('checkView', viewList[i]);
                    //TODO 跳转到景点列表
                });
                cardCheck.item(i).addEventListener('click', function () {
                    CommonUtil.setCookie('checkCard', JSON.stringify(cardList[i]));
                    window.location.href = 'forum_inside.html';
                })
            })(i);
        }
    }

    function initGeoInfo() {
        Api.ajax({
            method: 'GET',
            url: 'http://restapi.amap.com/v3/ip',
            data: {
                key: '405d4f074880ea8c53bd5f5022890841',
                output: 'JSON'
            },
            success: function (response) {
                console.log(response);
                currentCity = response.city;
                location.innerHTML = currentCity;
                // getViewList();
                getCardList();
                // getFoodList();
            }
        })
    }

    function initCard() {
        for (var i = 0; i < 2; i++) {
            var image = cardList[i].card.img.split(';')[0];
            cardImg.item(i).src = image;
            cardContent.item(i).innerHTML = cardList[i].card.content;
        }
    }

    //获取帖子列表
    function getCardList() {
        Api.ajax({
            method: 'POST',
            url: 'CardsWithCity',
            data: {
                site: currentCity,
                page: page,
                maxPage: 3
            },
            success: function (response) {
                if (response.code == 0) {
                    console.log(response);
                    cardList = response.data;
                    initCard();
                } else {
                    console.log(response.message);
                }
            }
        })
    }

    function initView() {
        for (var i = 0; i < 2; i++) {
            viewImg.item(i).src = viewList[i].imgurl;
            viewName.item(i).innerHTML = viewList[i].title;
        }
    }

    //获取景点列表
    function getViewList() {
        Api.ajax({
            method: 'POST',
            url: 'getViewInfoList',
            data: {
                city: currentCity,
                page: page
            },
            success: function (response) {
                if (response.error_code == 0) {
                    viewList = response.result;
                    initView();
                } else {
                    console.log(response.reason);
                }
            }
        });
        page++;
    }

    function initFood() {
        for(var i = 0; i < 2 ; i++){
            foodImg.item(i).src = foodList[i].photos;
            foodName.item(i).innerHTML= foodList[i].name;
            foodContent.item(i).innerHTML = foodList[i].address;
        }
    }

    //获取美食列表
    function getFoodList() {
        ajax({
            method: 'POST',
            url: 'getFoodInfoList',
            data: {
                city: currentCity,
                page: page,
                maxPage: 2
            },
            success: function (response) {
                if (response.resultcode == 0) {
                    foodList = response.result;
                    initFood();
                } else {
                    console.log(response.reason);
                }
            }
        })
    }

    initMap();
    initGeoInfo();
    initListener();
})();